package org.jeecg.modules.lab.util;

import org.jeecg.modules.lab.entrust.entity.*;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ReportData {


    public Map getMap(Object labWentrustsd, String params, Map<String, String> userList, LabItemsRecordReport
            labItemsRecordReport, List<LabEntrustItems> labEntrustItems, LabEntrustSample sample) throws
            IOException {
        Map map = new HashMap();
        Map mapR = new HashMap();
        String path = "entrust.xml";
        ;
        if (params.equals("委托检验")) {
            LabWentrust labWentrust = (LabWentrust) labWentrustsd;
            getNaturalGas(mapR, labWentrust, userList, labItemsRecordReport, sample);
            mapR.put("reportType", "委托检验");
        } else {
            LabWsupervise labWentrustsdss = (LabWsupervise) labWentrustsd;
            getNaturalGas2(mapR, labWentrustsdss, userList, labItemsRecordReport, sample);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            //todo 生产日期
            if (mapR.get("tailDate") != null && mapR.get("tailDate") != "") {
                mapR.put("tailDate", sdf1.format(mapR.get("tailDate")));
            } else {
                mapR.put("tailDate", "");
            }
            //todo 抽样时间
            if (mapR.get("spotDate") != null && mapR.get("spotDate") != "") {
                mapR.put("spotDate", sdf1.format(mapR.get("spotDate")));
            } else {
                mapR.put("spotDate", "");
            }
            //todo 入库日期
            if (mapR.get("tailWareDate") != null && mapR.get("tailWareDate") != "") {
                mapR.put("tailWareDate", sdf1.format(mapR.get("tailWareDate")));
            } else {
                mapR.put("tailWareDate", "");
            }
            mapR.put("reportType", "监督检验");
        }
        map.put("data", mapR);
        map.put("path", path);
        return map;
    }

    public void getNaturalGas(Map map, LabWentrust labWentrust, Map<String, String> userList, LabItemsRecordReport
            recordReport, LabEntrustSample oneSample) throws IOException {


        // todo 检验报告 首页
        map.put("reportCode", recordReport.getReportNum());
        String sampleName = recordReport.getSamName();
        map.put("sampleName", sampleName);
        if(sampleName.length()>=25){
            String samNameA = sampleName.substring(0,24);
            map.put("sampleNameA", samNameA);
            String samNameB = sampleName.substring(24);
            map.put("sampleNameB", samNameB.trim());
        }else {
            map.put("sampleNameA", sampleName);
            map.put("sampleNameB", "");
        }
        map.put("entName", labWentrust.getEntName());

        map.put("sampleBrand", handData(labWentrust.getSampleBrand()));
        map.put("samplePack", handData(labWentrust.getSamplePack()));
        map.put("samFactor", handData(labWentrust.getSampleFactory()));
        map.put("samSite", handData(labWentrust.getSampleSite()));// spot_address
        map.put("sampleGrade", handData(labWentrust.getSampleGrade()));

        map.put("samBase", handData(oneSample.getSamBase())); //spot_base
        map.put("handler",  handData(labWentrust.getHandler()));
        map.put("handlDate",  handData(labWentrust.getHandlDate()));
        map.put("samNum", handData(recordReport.getSamNum()));
        map.put("contacter", handData(labWentrust.getContacter()));
        map.put("sampleNumber", "--");
        map.put("sampleDate",  handData(labWentrust.getSampleDate()));
        map.put("sampleStandard", "--");
        map.put("samAppear", handData(oneSample.getSamAppear()));
        map.put("basisName",  handData(recordReport.getBasisName()));
        map.put("basisNum",  handData(recordReport.getBasisNum()));
        map.put("reportConcl", recordReport.getReportConcl());
        map.put("remarkss1", handData(recordReport.getRemark()));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        map.put("reportDate", sdf.format(recordReport.getReportDate()));
        map.put("ratifyName", recordReport.getRatifyName());
        map.put("auditName", recordReport.getAuditName());
        map.put("checkName", recordReport.getCheckName());

        map.put("reportNum", labWentrust.getReportNum());
        map.put("sampleType", labWentrust.getSampleType());

        map.put("sampleStatus",  handData(oneSample.getSamAppear()));
        map.put("samName", oneSample.getSamName());
        map.put("model", handData(oneSample.getModel()));
        map.put("samQuality",  handData(oneSample.getSamQuality()));
        map.put("samEnt", labWentrust.getEntName());
        Date deliverData = labWentrust.getDeliverData();
        if (deliverData != null) {
            map.put("deliverDatas", sdf.format(deliverData));
        } else {
            map.put("deliverDatas", "");
        }
        map.put("lotNum", oneSample.getLotNum());
        if ("待编辑".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
        } else if ("待审核".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        } else if ("待批准".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "finalAudits1", userList.get("finalAudits1"));
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        } else {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "firstAudits1", userList.get("firstAudits1"));
//            addSignature(map, "finalAudits1", userList.get("finalAudits1"));
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        }
        //todo 做null处理
        ObjectToMapUtil.dataToExcel(labWentrust, map);
        map.put("sampleNum", handData(String.valueOf(oneSample.getSamQuality())));
        String testItems = labWentrust.getTestItems();
        String substring = testItems.substring(1, testItems.length() - 1);
        String items = substring.replaceAll("\"", "").replaceAll(",", "、");
        map.put("testItems", items);

    }


    public void addSignature(Map map, String name, String realPath) throws IOException {
        File file = new File(realPath);
        FileInputStream fin4 = new FileInputStream(file);
        byte[] bytes4 = new byte[fin4.available()];
        fin4.read(bytes4);
        fin4.close();
        BASE64Encoder encoder4 = new BASE64Encoder();
        map.put(name, encoder4.encode(bytes4));
    }

    /**
     * 监督抽查
     *
     * @param map
     * @param labWsupervise
     * @throws ParseException
     */
    public void getNaturalGas2(Map map, LabWsupervise labWsupervise, Map<String, String> userList, LabItemsRecordReport
            recordReport, LabEntrustSample oneSample) throws IOException {
        // todo 检验报告 首页
        map.put("reportCode", recordReport.getReportNum());
        String sampleName = recordReport.getSamName();
        map.put("sampleName", sampleName);
        if(sampleName.length()>=25){
            String samNameA = sampleName.substring(0,24);
            map.put("sampleNameA", samNameA);
            String samNameB = sampleName.substring(24);
            map.put("sampleNameB", samNameB.trim());
        }else {
            map.put("sampleNameA", sampleName);
            map.put("sampleNameB", "");
        }
        map.put("entName", labWsupervise.getEntName());

        map.put("sampleBrand", handData(String.valueOf(labWsupervise.getTailBrand())));
        map.put("samplePack", handData(labWsupervise.getTailPackage()));
        map.put("samFactor", handData(String.valueOf(labWsupervise.getTailFactory())));
        map.put("samSite", handData(String.valueOf(labWsupervise.getSampleSort())));// spot_address
        map.put("sampleGrade", handData(String.valueOf(labWsupervise.getTailRank())));
        map.put("samBase", handData(String.valueOf(oneSample.getSamBase()))); //spot_base
        map.put("handler", labWsupervise.getHandler());
        map.put("handlDate", handData(labWsupervise.getHandlDate()));
        map.put("samNum", handData(String.valueOf(recordReport.getSamNum())));
        map.put("contacter", handData(labWsupervise.getSpotPerson()));
        map.put("sampleNumber", handData(String.valueOf(labWsupervise.getTailCode())));
        map.put("sampleDate", labWsupervise.getHandlDate());
        map.put("sampleStandard", handData(String.valueOf(labWsupervise.getSpotStandard())));
        map.put("samAppear", handData(String.valueOf(oneSample.getSamAppear())));
        map.put("basisName",  handData(String.valueOf(recordReport.getBasisName())));
        map.put("basisNum", handData(String.valueOf(recordReport.getBasisNum())));
        map.put("reportConcl", recordReport.getReportConcl());
        map.put("remarkss1", recordReport.getRemark());
        map.put("remarkJD", labWsupervise.getRemark());

        SimpleDateFormat sdfB = new SimpleDateFormat("yyyy年MM月dd日");
        map.put("reportDate", sdfB.format(recordReport.getReportDate()));
        map.put("ratifyName", recordReport.getRatifyName());
        map.put("auditName", recordReport.getAuditName());
        map.put("checkName", recordReport.getCheckName());

        map.put("reportNum", recordReport.getReportNum());
        map.put("sampleType", labWsupervise.getSampleType());

        map.put("sampleStatus", oneSample.getSamAppear());
        map.put("samName", oneSample.getSamName());
        map.put("model", oneSample.getModel());
        map.put("samQuality", String.valueOf(oneSample.getSamQuality()));
        map.put("samEnt", String.valueOf(labWsupervise.getEntName()));

        map.put("deliverDatas", "");
        map.put("lotNum", oneSample.getLotNum());
        if ("待编辑".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
        } else if ("待审核".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        } else if ("待批准".equals(recordReport.getBillStatus())) {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "finalAudits1", userList.get("finalAudits1"));
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        } else {
            map.put("firstAudits1", "");
            map.put("finalAudits1", "");
            map.put("editorChiefs2", "");
//            addSignature(map, "firstAudits1", userList.get("firstAudits1"));
//            addSignature(map, "finalAudits1", userList.get("finalAudits1"));
//            addSignature(map, "editorChiefs2", userList.get("editorChiefs2"));
        }
        //todo 做null处理
        ObjectToMapUtil.dataToExcel(labWsupervise, map);
        map.put("sampleNum", handData(String.valueOf(oneSample.getSamQuality())));
        String testItems = labWsupervise.getTestItems();
        String substring = testItems.substring(1, testItems.length() - 1);
        String items = substring.replaceAll("\"", "").replaceAll(",", "、");
        map.put("testItems", items);

    }

    /**
     * 把签名保存到本地
     *
     * @return
     */
    public String upSignature(String name, String thpt) {
        //图片位置
        String path = "D:\\zhuiExperiment\\picture\\";
        //String imageUrl = "http://11.89.24.203:9008/f/2023-11-30/personsign1701340825442_20231130184026157_20231130184026188_116912.png";
        String filePath = "";
        try {
            URL url = new URL(thpt);
            InputStream inputStream = url.openStream();
            // 创建本地文件路径和文件名
            filePath = path + name + ".jpg";
            // 创建文件输出流
            FileOutputStream outputStream = new FileOutputStream(new File(filePath));
            // 读取输入流中的数据，并写入到输出流中
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            // 关闭输入流和输出流
            inputStream.close();
            outputStream.close();
            System.out.println("图片保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }


    /**
     * 处理数据
     *
     * @param str
     * @return
     */
    private String handData(String str) {
        if (str == null || "".equals(str)) {
            str = "--";
        }
        return str;
    }

}
